﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            eventwriter();
            checkIfStartSet();
            // this will make sure you get the first item selected
          domainUpDown1.SelectedIndex = 0 ;
         domainUpDown2.SelectedIndex = 0 ;
         domainUpDown3.SelectedIndex = 0 ;
            //set the status of the status bar
         statusbarstatus();
         WindowsFormsApplication1.Form2 j = new WindowsFormsApplication1.Form2();
             j. createLog(1);
             richTextBox1.Text = File.ReadAllText("Log1.txt");
             HighlightLastLine(richTextBox1, Color.Gold);
    }
        public void HighlightLastLine(RichTextBox TextControl, Color HighlightColor)
        {
            TextControl.Text = TextControl.Text.Trim();
            TextControl.SelectionStart = 0;
            TextControl.SelectionLength = 0;
            TextControl.SelectionColor = Color.Black;
            string LastLineText = TextControl.Lines[richTextBox1.Lines.Count() - 1];
            int LastLineStartIndex = richTextBox1.Text.LastIndexOf(LastLineText);
            TextControl.SelectionStart = LastLineStartIndex;
            TextControl.SelectionLength = TextControl.Text.Length - 1;
            TextControl.SelectionColor = HighlightColor;
        }

        //minimise to tray
        //private void Form1_Resize(object sender, EventArgs e)
        //{
        //    if (FormWindowState.Minimized == this.WindowState)
        //    {
        //        mynotifyicon.Visible = true;
        //        mynotifyicon.ShowBalloonTip(500);
        //        this.Hide();
        //    }

        //    else if (FormWindowState.Normal == this.WindowState)
        //    {
        //        mynotifyicon.Visible = false;
        //    }
        //}
       
        
      public void checkIfStartSet()
      {   if (rkApp.GetValue("MyApp") == null)
            {
                // The value doesn't exist, the application is not set to run at startup
                chkRun.Checked = false;
            }
            else
            {
                // The value exists, the application is set to run at startup
                chkRun.Checked = true;
            }
}

        public void reged()
        {
         
            if (chkRun.Checked)

            {
                Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\rkApp");
                // Add the value in the registry so that the application runs at startup
                rkApp.SetValue("MyApp", Application.ExecutablePath.ToString());
            }
            else
            {
                // Remove the value from the registry so that the application doesn't start
                rkApp.DeleteValue("MyApp", false);
            }
        }
        public void eventwriter ()
        {            // Create the source, if it does not already exist.
            
            
        if(!EventLog.SourceExists("Terminator2"))
        {
             //An event log source should not be created and immediately used.
             //There is a latency time to enable the source, it should be created
             //prior to executing the application that uses the source.
             //Execute this sample a second time to use the new source.
            EventLog.CreateEventSource("Terminatorx", "StatLog");
            //Console.WriteLine("CreatedEventSource");
            //Console.WriteLine("Exiting, execute the application a second time to use the source.");
            // The source is created.  Exit the application to allow it to be registered.
            return;
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "Terminatorx";

        // Write an informational entry to the event log.    

            
            myLog.WriteEntry("Application Terminator X has been started.");
        }


        

        private void button1_Click(System.Object sender,
                            System.EventArgs e)
        {
            // Add the text box contents and initial location in the collection
            // to the DomainUpDown control.
            int myCounter = 1;
            domainUpDown1.Items.Add((textBox1.Text.Trim()) + " - " + myCounter);

            // Increment the counter variable.
            myCounter = myCounter + 1;

            // Clear the TextBox.
            textBox1.Text = "";
        }

        private void checkBox1_Click(System.Object sender,
                                    System.EventArgs e)
        {
            // If Sorted is set to true, set it to false; 
            // otherwise set it to true.
            if (domainUpDown1.Sorted)
            {
                domainUpDown1.Sorted = false;
            }
            else
            {
                domainUpDown1.Sorted = true;
            }
        }

    

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void domainUpDown3_SelectedItemChanged(object sender, EventArgs e)
        {

        }

        private void domainUpDown2_SelectedItemChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void EventLog_EntryWritten(object sender, EntryWrittenEventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            eventwriter();
            reged();
        }

        private void label2_Click_1(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)

        {
            WindowsFormsApplication1.CreateLogClass j = new WindowsFormsApplication1.CreateLogClass();
            j.runBatch();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            WindowsFormsApplication1.CreateLogClass j = new WindowsFormsApplication1.CreateLogClass();
            j.EableUAc();
        }

        //private void domainUpDown1_SelectedItemChanged_1(object sender, EventArgs e)
        //{
          
        //}

        private void getUpDown_Click(object sender, EventArgs e)
        {
            
        string hours;
        string minutes;
        string seconds;
        string datestart;
        DateTime setdate;
        //DateTime nextdate;
            DateTime nownow;
        setdate = dateTimePicker1.Value.Date; 
           
            DateTime dt = DateTime.Parse("6/22/2009 07:00:00 AM");

            dt.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits

            DateTime theDate = DateTime.UtcNow;
            nownow = DateTime.Now.Date;

            //nextdate = DateTime.Compare(theDate, nownow);



//get the string form the up and down text
        hours = domainUpDown1.Text.ToString();
        minutes = domainUpDown2.Text.ToString();
        seconds = domainUpDown3.Text.ToString();
         datestart = dateTimePicker1.Value.Date.ToString();
            //or to short date
        // string shortDate = dateTimePicker1.Value.ToShortDateString();


        MessageBox.Show("set the shutdown time to " + hours+":"+ minutes + ":" + seconds+"/n and the start date to " + datestart);
       
      
        }
        public void statusbarstatus()
        {
            string status;
            string isset = "  Computer is set to restart  ";

            status = DateTime.Now.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
            if (chkRun.Checked == true)
            {


                status = status + isset;
            }
            // if checkbox checkded alert 
            // if not just show time

            toolStripStatusLabel1.Text = status;
         
         
            DateTime myDate = DateTime.Today;
            DateTime tomorrowAtNoon = myDate.AddDays(1).AddHours(12);
            string s = tomorrowAtNoon.ToString();

            toolStripStatusLabel1.Text = status + s;
         
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
           Form a = new Form2();
            a.Show();
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Form a = new AboutBox1();
            a.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Close();
        }



        internal void refreshrichtext()
        {
            richTextBox1.Text = File.ReadAllText("Log1.txt");
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
         //   Application.Exit();
        }
        private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
        {

          //  Show();

          //  WindowState = FormWindowState.Normal;

        }
       
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}


